/**
 * Created by Laggo on 11/5/15.
 */
app.directive("choseproduct", ['productCategoryService', 'commodityService','toolService', function (productCategoryService, commodityService,toolService) {
    return {
        restrict: 'E',
        templateUrl: 'www/html/directive/choseProduct.html',
        replace: true,
        transclude: true,
        scope: {
            commoditys:'='
        },
        link: function (scope, ele, attr) {

            //第一次加载一级类目
            productCategoryService.list({
                level: 1
            }).then(function (e) {
                scope.listData = e;
            })

            scope.commoditys = [];


            //加载二级类目
            scope.list2 = function () {
                productCategoryService.list({
                    parentId: scope.category1
                }).then(function (e) {
                    scope.listData2 = e;
                })
            }

            scope.parm = {
                categoryId: '',
                currentPage: 1,
                name_like: '',
                pageSize: 10
            }


            scope.submit = function (e) {
                scope.parm.currentPage = e;
                commodityService.list(scope.parm)
                    .then(function (result) {
                       //console.log(result);
                        scope.parm.totalSize = result.totalSize;
                        scope.commodityList = result.resultData;
                    })
            }

            scope.submit(1);

            scope.chose = function (id, name,outPrice) {
                var obj = {
                    commodityId: id,
                    name: name,
                    number: 1,
                    outPrice:outPrice
                }
                if(!toolService.inArray(id,scope.commoditys)){
                    scope.commoditys.push(obj);
                }
            }
        },
    }
}]);
